﻿//Main game class: runs game loop, handles buttons
package code {
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.ui.Keyboard;
	import code.KeyboardManager;

	// Main game class the runs the game
	public class MainGame extends MovieClip {		
		private var board:GameBoard;
		
		private var gamePaused:Boolean;
		private var keyboardManager:KeyboardManager;
		//Constructor
		public function MainGame() 
		{
			gamePaused = false;
			
			Play_Btn.addEventListener(MouseEvent.CLICK, startGame);				
			Instruct_Btn.addEventListener(MouseEvent.CLICK, showInstruct);	
			stage.focus = this;
			keyboardManager = new KeyboardManager(this);
			keyboardManager.isActive = true;
		}	
		
		//Begin the game, add listeners for buttons on game screen
		public function startGame(e:Event):void
		{
			this.gotoAndPlay("Game"); //Move to the Game screen
			
			//Set up buttons
			ReturntoMenu_Btn.addEventListener(MouseEvent.CLICK, returnToMenu);
			StartGame_Btn.addEventListener(MouseEvent.CLICK, moveGame);
			Pause_Btn.addEventListener(MouseEvent.CLICK, pauseGame);
			Restart_Btn.addEventListener(MouseEvent.CLICK, restartGame);
			
			//Create the game
			board = new GameBoard();
			addChild(board);				
			
			//Start Game loop
			addEventListener(Event.ENTER_FRAME, update);
						

			//stage.addEventListener(KeyboardEvent.KEY_DOWN, keyD);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyU);
		}
				
		//Main game loop
		public function update(e:Event){
			keyD();
			board.updateGame();//Update the game
			
			//Update Scores
			score_Box.text = "" + board.ball.score;
			balls_Box.text = "" + board.ball.ballsLeft;
			
			//Check for Game Over
			if(board.ball.ballsLeft < 0)
			{
				//Remove old board
				removeEventListener(Event.ENTER_FRAME, update);				
				removeChild(board);
				
				this.gotoAndPlay("End");			
				
				//Listen for buttons
				Menu_Btn.addEventListener(MouseEvent.CLICK, goToMenu);	
				RestartAll_Btn.addEventListener(MouseEvent.CLICK, startGame);				
			}		
		}
		
		//Navigate to the Instructions screen
		public function showInstruct(e:Event):void
		{
			this.gotoAndPlay("Instructions");
			Menu_Btn.addEventListener(MouseEvent.CLICK, goToMenu);	
		}
		
		//Navigate back to the Main Menu from instructions screen
		public function goToMenu(e:Event):void
		{
			this.gotoAndPlay("Start");
			Play_Btn.addEventListener(MouseEvent.CLICK, startGame);
			Instruct_Btn.addEventListener(MouseEvent.CLICK, showInstruct);
		}
		
		//Naviagte to the Main Menu from the Game, End screens
		public function returnToMenu(e:Event):void
		{
			//Stop the game
			removeChild(board);
			
			this.gotoAndPlay("Start");
			Play_Btn.addEventListener(MouseEvent.CLICK, startGame);
			Instruct_Btn.addEventListener(MouseEvent.CLICK, showInstruct);
		}	
		
		//Pause the game
		public function pauseGame(e:Event):void
		{
			if(gamePaused == false)
			{
				removeEventListener(Event.ENTER_FRAME, update); //Stop all updates				
				gamePaused = true;
			}
			
			else
			{
				addEventListener(Event.ENTER_FRAME, update);
				gamePaused = false;
			}
		}
		
		//Reset the game to defaults
		public function restartGame(e:Event):void
		{
			removeChild(board); //Remove the old board			
			
			 //In case the game is paused when reset
			 if(gamePaused == true)
			 {
				addEventListener(Event.ENTER_FRAME, update);
				gamePaused = false;
			 }
			
			//Set up a new board
			board = new GameBoard();
			addChild(board);				
		}	
		
		//Start game from pause
		public function moveGame(e:Event):void
		{
			addEventListener(Event.ENTER_FRAME, update);
			gamePaused = false;
		}
		
		// handles key pressed events. flippers dont work yet
		public function keyD():void
		{		
			if(keyboardManager.left){
				board.leftFlipper.rotateFlipper(-Math.PI / 6);
			}
			if(keyboardManager.right){
				board.rightFlipper.rotateFlipper((7 * Math.PI) / 6);
			}
			if(keyboardManager.space){
				if(!board.ball.hasLaunched)  // if the ball has not been launched yet it launches
					board.ball.launch();
			}
		}
		
		public function keyU(event:KeyboardEvent):void{
			switch(event.keyCode){
				case Keyboard.LEFT:
					board.leftFlipper.rotated = false;
					break;
				case Keyboard.RIGHT:
					board.rightFlipper.rotated = false;
					break;
			}
		}
	}
}
